﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using VOL;
using System.Data;
using DAL;

namespace BLL
{
    public class HoaDonBanBL
    {
        private DataAccess dataAccess;
        public HoaDonBanBL()
        {
            dataAccess = new DataAccess();
        }

        //Thêm
        public bool insertHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgHD", SqlDbType.DateTime) { Value = obj.NgHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.VarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Bit) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgDuyet", SqlDbType.DateTime) { Value = obj.NgDuyet };

            string strSql = "INSERT INTO tbl_HoaDonBan(MaKhach, NgHD, DienGiai, Duyet, NgDuyet) VALUES (@MaKhach, @NgHD, @DienGiai, @Duyet, @NgDuyet)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        //Xóa
        public bool deleteHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "DELETE FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Sửa
        public bool updateHoaDonBan(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgHD", SqlDbType.DateTime) { Value = obj.NgHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.VarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Bit) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgDuyet", SqlDbType.DateTime) { Value = obj.NgDuyet };
            param[5] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "UPDATE tbl_HoaDonBan SET " +
                            "MaKhach = @MaKhach, " +
                            "NgHD = @NgHD, " +
                            "DienGiai = @DienGiai, " +
                            "Duyet = @Duyet " +
                            "NgDuyet = @NgDuyet " +
                            "WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Tìm
        public DataTable searchIDHoaDonBan(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };

            string strSql = "SELECT * FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            return dataAccess.GetData(strSql, param);
        }

        public DataTable getDataReport(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };

            string strSql = "SELECT  tbl_ChiTietHoaDon.MaHD, SUM(tbl_ChiTietHoaDon.DonGia * tbl_ChiTietHoaDon.SoLuong) AS ThanhTien, tbl_HoaDonBan.NgHD, tbl_DMKhach.HoTen, tbl_DMKhach.DiaChi, tbl_DMKhach.DienThoai, tbl_DMKhach.Email " +
                        "FROM  tbl_ChiTietHoaDon INNER JOIN tbl_HoaDonBan ON tbl_ChiTietHoaDon.MaHD = tbl_HoaDonBan.MaHD INNER JOIN tbl_DMKhach ON tbl_HoaDonBan.MaKhach = tbl_DMKhach.MaKhach " +
                        " WHERE tbl_ChiTietHoaDon.MaHD = @MaHD " +
                        " GROUP BY tbl_ChiTietHoaDon.MaHD, tbl_HoaDonBan.NgHD, tbl_DMKhach.HoTen, tbl_DMKhach.DiaChi, tbl_DMKhach.DienThoai, tbl_DMKhach.Email ";

            return dataAccess.GetData(strSql, param);
        }

        public int GetMaHoaDonNew()
        {
            string strSql = "SELECT TOP (1) MaHD FROM tbl_HoaDonBan ORDER BY MaHD DESC";
            return dataAccess.GetNumberData(strSql);
        }

        public DataTable GetDataHangHoaDaBan()
        {
            string strSql = "SELECT COUNT(tbl_ChiTietHoaDon.MaHang) AS SoLuongBan, tbl_DMHang.TenHang, tbl_NhaCungCap.TenNhaCungCap FROM tbl_ChiTietHoaDon INNER JOIN " +
                         " tbl_DMHang ON tbl_ChiTietHoaDon.MaHang = tbl_DMHang.MaHang INNER JOIN "+
                         " tbl_NhaCungCap ON tbl_DMHang.MaNhaCungCap = tbl_NhaCungCap.MaNhaCungCap " +
                            " GROUP BY tbl_DMHang.TenHang, tbl_NhaCungCap.TenNhaCungCap";
            return dataAccess.GetData(strSql);
        }
    }
}
